require(["common", "jQuery", "coAjax", "queryString", "hash_routie"], function(jSouper, $, coAjax, QueryString, hash_routie) {
	var ak_key = "rroTv2TmcdKGCiQ2jER1oA3V";
	var doc_write = document.write;
	var map; //定义全局map对象
	var map_marker; // 定义全局标注点对象
	var doc_s_write = function(HTML) {
		var dom = $(HTML);
		console.log("heml" + HTML);
		dom.find("script[src]").each(function(i, scriptNode) {
			scriptNode.src && require([scriptNode.src]);
			scriptNode.removeAttribute("src")
		});
		dom.appendTo(document.body);
	}
	document.write = doc_s_write;
	require(["http://api.map.baidu.com/api?v=2.0&ak=Noga0lSLMrk62roymqXkgaUB"], function(scriptText) {
		document.write = doc_write;
		var _BMap_ti = setInterval(function() {
			if (window.BMap) {
				clearInterval(_BMap_ti);
				init_map();
			}
		});
	});


	// 加载地图并地位到中心位置
	function init_map() {
		// 百度地图API功能
		map = new BMap.Map("store_map"); // 创建Map实例
		map.addControl(new BMap.NavigationControl({
			anchor: BMAP_ANCHOR_TOP_RIGHT,
			type: BMAP_NAVIGATION_CONTROL_SMALL
		})); //添加地图缩放控件	
		map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
		var geolocation = new BMap.Geolocation();
		geolocation.getCurrentPosition(function(r) {
			if (this.getStatus() == BMAP_STATUS_SUCCESS) {
				map_marker = new BMap.Marker(r.point);
				App.set("$Cache.store_map.new_point.lng", r.point.lng);
				App.set("$Cache.store_map.new_point.lat", r.point.lat);
				map_marker.enableDragging(); // 允许标注点拖拽
				map.centerAndZoom(r.point, 14);
				map.addOverlay(map_marker);
				// 在标注点拖拽完成后触发获取标注点坐标事件
				map_marker.addEventListener("dragend", function(e) {
					// map_marker.setAnimation(BMAP_ANIMATION_DROP); //拖拽后标注点跳动动画
					App.set("$Cache.store_map.new_point.lng", map_marker.point.lng);
					App.set("$Cache.store_map.new_point.lat", map_marker.point.lat);
				});
				// 点击地图时候触发此事件，将标注点移动到点击位置
				map.addEventListener("click", function(e) {
					App.set("$Cache.store_map.new_point.lng", e.point.lng);
					App.set("$Cache.store_map.new_point.lat", e.point.lat);
					map_marker.point.lng = e.point.lng;
					map_marker.point.lat = e.point.lat;
					map.addOverlay(map_marker);
				});
			} else {
				alert('failed' + this.getStatus());
			}
		}, {
			enableHighAccuracy: true
		});

	};

	// 地图定位检索
	App.set("$Event.store_map.key_word_search", function() {
		var local = new BMap.LocalSearch(map, {
			renderOptions: {
				map: map,
				autoViewport: true
			}
		});
		local.search(App.get("$Cache.store_map.key_word_search"));
	});

	// 图片相关处理模块
	require(["lrz"], function() {
		var logo_lrz_config = {
			width: 500,
			quality: 1
		};
		var store_imgs_lrz_config = {
			width: 1000,
			quality: 1
		};
		// 添加logo
		App.set("$Event.store_map.new_point.add_logo", function() {
			var logo_file = this.files[0];
			upload_img_get_url(logo_file, logo_lrz_config).then(function(img_url_data) {
				App.set("$Cache.store_map.new_point.logo_url", appConfig.img_server_url + img_url_data.key);
			});
		});

		// 添加店铺图片
		App.set("$Event.store_map.new_point.add_store_img", function() {
			var store_imgs_length = App.get("$Cache.store_map.new_point.store_imgs.length");
			if (store_imgs_length && (store_imgs_length > 7)) {
				this.files = [];
				alert("warn", "店铺图片不能多于8张");
				return;
			};
			var img_file = this.files[0];
			upload_img_get_url(img_file, store_imgs_lrz_config).then(function(new_store_img_url) {
				App.concat("$Cache.store_map.new_point.store_imgs", [appConfig.img_server_url + new_store_img_url.key]);
				alert("success", "图片添加成功");
			});
		});

		// 删除店铺图片
		App.set("$Event.store_map.remove_store_img", function(e, vm) {
			App.get("$Cache.store_map.new_point.store_imgs").splice(vm.get("$Index"), 1);
			App.set("$Cache.store_map.new_point.store_imgs", App.get("$Cache.store_map.new_point.store_imgs"));
			alert("success", "删除成功！");
		});

		// 图片上传通用方法
		var upload_img_get_url = function(img_file, lrz_config) {
			return new Promise(function(resolve, reject) {
				lrz(img_file, lrz_config, function(result) {
					coAjax.post(appConfig.other.upload_base64_image, {
						img_base64: result.base64
					}, function(img_result) {
						resolve(img_result.result);
						console.log(img_result);
					});
				});
			});
		};
	});

	var remote_server_url = "http://121.40.18.23:1518/api/complex/";
	// var remote_server_url = "http://localhost:1518/api/complex/";

	hash_routie.on("store_map", function() {
		// 获取店铺信息列表
		$.getJSON(remote_server_url + "StoreMapFactory", function(result) {
			console.log(result);
			App.set("$Cache.store_map.bus_store_list", result || []);
		});
	});

	// 上传创建新的店铺信息
	App.set("$Event.store_map.add_new_point", function() {
		var map_point = create_point_to_upload();
		if (!map_point) {
			return;
		};
		$.post(remote_server_url + "StoreMapFactory", map_point, function(result) {
			console.log("result", result);
			App.get("$Cache.store_map.bus_store_list") ? App.get("$Cache.store_map.bus_store_list").unshift(result) : App.set("$Cache.store_map.bus_store_list", [result]);
			App.set("$Cache.store_map.bus_store_list", App.get("$Cache.store_map.bus_store_list"));
			App.set("$Cache.store_map.new_point", {});
		});
	});

	// 打开店铺信息修改
	var $window = $(window);
	App.set("$Event.store_map.open_update", function(e, vm) {
		// 重新绑定表单
		App.set("$Cache.store_map.new_point.title", vm.get("title"));
		App.set("$Cache.store_map.new_point.address", vm.get("address"));
		App.set("$Cache.store_map.new_point.lng", vm.get("longitude"));
		App.set("$Cache.store_map.new_point.lat", vm.get("latitude"));
		App.set("$Cache.store_map.new_point.logo_url", vm.get("logo_url"));
		App.set("$Cache.store_map.new_point.phone", vm.get("phone"));
		App.set("$Cache.store_map.new_point.mobile", vm.get("mobile"));
		App.set("$Cache.store_map.new_point.description", vm.get("description"));
		App.set("$Cache.store_map.new_point.store_imgs", vm.get("store_imgs") ? (vm.get("store_imgs")).split(",") : []);
		// 绑定id
		App.set("$Cache.store_map.on_update_point", vm.get("geotable_id"));
		// 重新设定标注点位置
		map_marker.point.lng = vm.get("longitude");
		map_marker.point.lat = vm.get("latitude");
		map.centerAndZoom(new BMap.Point(vm.get("longitude"), vm.get("latitude")), 14);
		map.addOverlay(map_marker);
		// 打开修改权限
		App.set("$Cache.store_map.is_on_update", true);
		$window.scrollTop(0);
	});

	// 提交店铺信息修改
	App.set("$Event.store_map.update_new_point", function() {
		var map_point = create_point_to_upload();
		if (!map_point) {
			return;
		};
		map_point.geotable_id = App.get("$Cache.store_map.on_update_point");
		coAjax.put(remote_server_url + "StoreMapFactory", map_point, function(result) {
			alert("success", "修改成功！！！");
			App.set("$Cache.store_map.new_point", {});
		});
	});

	// 取消店铺信息修改
	App.set("$Event.store_map.cancel_update_new_point", function() {
		App.set("$Cache.store_map.new_point", {});
		App.set("$Cache.store_map.on_update_point", "");
		App.set("$Cache.store_map.is_on_update", false);
	});

	// 删除店铺信息点
	App.set("$Event.store_map.delete_store_point", function(e, vm) {
		myConfirm("确定要删除此店铺信息吗？", function() {
			var map_point = {
				geotable_id: vm.get("geotable_id")
			};
			coAjax.delete(remote_server_url + "StoreMapFactory", map_point, function(result) {
				alert("success", "删除成功！！！");
				vm.remove();
			});
		});
	});

	// 创建提交表单
	function create_point_to_upload() {
		var map_point = {
			title: App.get("$Cache.store_map.new_point.title"),
			address: App.get("$Cache.store_map.new_point.address"),
			longitude: App.get("$Cache.store_map.new_point.lng"),
			latitude: App.get("$Cache.store_map.new_point.lat"),
			logo_url: App.get("$Cache.store_map.new_point.logo_url"),
			phone: App.get("$Cache.store_map.new_point.phone"),
			mobile: App.get("$Cache.store_map.new_point.mobile"),
			description: App.get("$Cache.store_map.new_point.description"),
			store_imgs: App.get("$Cache.store_map.new_point.store_imgs") || []
		};
		if (!map_point.title) {
			alert("warn", "请输入店铺名！！！");
			return;
		};
		if (!map_point.address) {
			alert("warn", "请输入地址详情！！！");
			return;
		};
		if (!map_point.logo_url) {
			alert("warn", "请上传店铺logo！！！");
			return;
		};
		if (!map_point.description) {
			alert("warn", "请输入店铺介绍！！！");
			return;
		};
		if (!(map_point.phone || map_point.mobile)) {
			alert("warn", "请输入电话或手机号！！！");
			return;
		};
		if (!map_point.store_imgs.length) {
			alert("warn", "请至少上传一张店铺图片！！！");
			return;
		};
		if (!(map_point.longitude || map_point.latitude)) {
			alert("warn", "请在地图上标注店铺位置！！！");
			return;
		};
		// map_point.store_imgs = map_point.store_imgs.toString();
		return map_point;
	};
});